package thread;

/**
 * @author Bboy Dday
 * @create 2020:06:15--20:13
 */

class Clerk{//店员

    //定义产品数量
    private int productCount = 0;

    public synchronized void produceProduct() {

        if ( productCount < 20 ){
            productCount++;
            System.out.println( Thread.currentThread().getName() + "：开始生产第" + productCount + "个产品" );
            notify();
        }else {
            //等待
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public synchronized void consumeProduct(){

        if ( productCount > 0 ){
            System.out.println( Thread.currentThread().getName() + "：开始消费第" + productCount + "个产品" );
            productCount--;

            notify();

        }else {
            //等待
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

class Consumer extends Thread{//消费者

    private Clerk clerk;

    public Consumer(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {

        System.out.println( Thread.currentThread().getName() + "：开始消费产品........" );
        while ( true ){
            try {
                Thread.sleep( 2500 );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.consumeProduct();
        }

    }

}

class Producer extends Thread{//生产者

    private Clerk clerk;

    public Producer(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {

        System.out.println( Thread.currentThread().getName() + "：开始生产产品........" );
        while ( true ){
            try {
                Thread.sleep( 1000 );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.produceProduct();
        }

    }
}

public class ProductTest {
    public static void main(String[] args) {

        Clerk clerk = new Clerk();
        Producer p1 = new Producer( clerk );//生产者1
        p1.setName( "生产者1号" );

        Consumer c1 = new Consumer( clerk );//消费者1
        c1.setName( "消费者1号" );
        Consumer c2 = new Consumer( clerk );//消费者2
        c2.setName( "消费者2号" );

        p1.start();
        c1.start();
        c2.start();

    }
}
